package frame.edit 
{
	import data.DataMonitor;
	import debug.ASSERT;
	import flash.display.DisplayObject;
	import frame.IFrame;
	import nsdecl.ns_bindattr_filter;
	import nsdecl.ns_dialog_service;
	import nsdecl.ns_group_filter;
	import nsdecl.ns_group_handler;
	import nsdecl.ns_group_hook;
	import nsdecl.ns_interactive_slot;
	import nsdecl.ns_vname_decl;
	import common.IData;
	import common.StructGroup;
	import common.StructStaff;
	import proxy.DataAccess;
	import ui.ui_button;
	import ui.ui_check_box;
	import ui.ui_icon_button;
	import ui.ui_scroll_bar;
	import ui.ui_text;
	import ui.ui_tree_tag;
	import ui.uiGroup;
	import ui.uiRadioGroup;
	import ui.uiUnit;
	import ui.uiWin;
	import ui.UserInput;
	import ui.utils.bindattrFormat;
	import ui.utils.dialogProperty;
	import ui.utils.initUIContainer;
	import ui.utils.setPosUnit;
	import vndecl.*;
	
	/**
	 * 编辑分组界面
	 * @author anod
	 */
	public class RecordGroup extends EditPage implements IFrame
	{
		use namespace ns_vname_decl;
		use namespace ns_dialog_service;
		
		private var mFrame:FRecordGroup;
		private var mEditingMember:Array;
		private var mExpandedList:uiGroup;
		private var mCurIdx:int;
		
		public function RecordGroup() 
		{
			mFrame = new FRecordGroup();
			addChild( mFrame );
			super();
			mEditingMember = [];
			mCurIdx = -1;
			
			setupInteractive();
			setupDataMonitor();
		}
		
		/* INTERFACE frame.IFrame */
		
		public function setupInteractive():void 
		{
			mExpandedList 			= new uiGroup();
			
			closeButton.click 		= "switchChild";
			finishButton.click 		= "switchChild";
			okButton.click			= "addEditedGroup";
			
			addButton.click 		= "addToThisGroup";
			delButton.click 		= "delFromThisGroup";
			
			groupList.handler 		= "showGroupNode";
			groupList.expand 		= "showGroupMember";
			
			mExpandedList.handler   = "listExpanded";
			mExpandedList.rule      = "5000,1,0,30";
			mExpandedList.child		= uiUnit;
		}
		
		public function setupDataMonitor():void 
		{
			DataMonitor.updateData( mEditingMember, VNPC.RECORDGROUP_EDITING );
			
			groupMembers.data 		= VNPC.RECORDGROUP_EDITING;
			groupMembers.handler 	= "listMember";
			
			groupMembers.hook       = "scrollable";
			groupBar.area           = "iGroupMembers";
			groupBar.scroll         = "scrollPlaceSprite";
			
			allMembers.data 		= DataAccess.instance.queryStaffInterf().vname;
			allMembers.handler 		= "listMember";
			allMembers.filter 		= "withoutEditing";
			
			allMembers.hook         = "scrollable";
			allBar.area             = "iAllMembers";
			allBar.scroll           = "scrollPlaceSprite";
			
			mExpandedList.data      = VNPC.RECORDGROUP_EXPANDED;
			
			groupList.bindAttr( "tags", bindattrFormat( DataAccess.instance.queryGroupInterf().vname, "setGroupTag" ) );
		}
		
		//主功能
		//------------------------------------
		protected function setToEditingMembers( member:Array ):void
		{
			mEditingMember.length = 0;
			var instance:DataAccess = DataAccess.instance;
			for each( var id:int in member )
			{
				mEditingMember.push( instance.queryStaffInterf().getByID(id) );
			}
		}
		
		//交互回调定义
		//------------------------------------
		
		/** 过滤：从所有值班人员中过滤出不包含在当前组里面的 */
		ns_group_filter function withoutEditing( index:int, value:StructStaff ):Boolean
		{
			return mEditingMember.indexOf(value) < 0;
		}
		
		/** 显示操作人员列表 */
		ns_group_handler function listMember( entry:uiUnit, value:StructStaff ):void
		{
			var content:ui_text = new ui_text();
			with ( content )
			{
				width = 120, height = 20, margin = 9, blank = 2;
				background = "img0307";
				text = value.name + "@" + value.identity;
			}
			content.show_display();
			content.panel.visible = false;
			entry.mouseChildren = false;
			entry.click = "checkOnMember";
			entry.addChild( content );
		}
		
		/** 能否滚动 */
		ns_group_hook function scrollable( group:uiGroup ):void
		{
			var ps:PlaceSprite = group.parent as PlaceSprite;
			var bar:PlaceSprite = dialogProperty( ps, ps.name.slice(0, -7) + "Bar", typeof(this) );
			bar.component.visible = ps.scrollRect.height < ps.component.height;
		}
		
		/** 选中某个人员 */
		ns_interactive_slot function checkOnMember( entry:uiUnit ):void
		{
			var content:ui_text = entry.getChildAt(0) as ui_text;
			content.panel.visible = !content.panel.visible;
		}
		
		/** 点击<<加入到该组 */
		ns_interactive_slot function addToThisGroup( button:ui_icon_button ):void
		{
			var i:int;
			var toBeAdded:Array = [];
			
			// 拿到全部的选中列表
			for ( i = 0; i < allMembers.numChildren; ++i )
			{
				var staff_unit:uiUnit = allMembers.getChildAt(i) as uiUnit;
				var content:ui_text = staff_unit.getChildAt(0) as ui_text;
				content.panel.visible && toBeAdded.push( staff_unit.data );
			}
			
			if ( toBeAdded.length > 0 )
			{
				mEditingMember = mEditingMember.concat( toBeAdded );
				DataMonitor.updateData( mEditingMember, VNPC.RECORDGROUP_EDITING );
				allMembers.onDataUdt( { } );
			}
		}
		
		/** 点击>>从分组删除人员 */
		ns_interactive_slot function delFromThisGroup( button:ui_icon_button ):void
		{
			var i:int;
			var new_list:Array = [];
			
			// 拿到全部的选中列表
			for ( i = 0; i < groupMembers.numChildren; ++i )
			{
				var staff_unit:uiUnit = groupMembers.getChildAt(i) as uiUnit;
				var content:ui_text = staff_unit.getChildAt(0) as ui_text;
				content.panel.visible == false && new_list.push( staff_unit.data );
			}
			
			if ( new_list.length < mEditingMember.length )
			{
				mEditingMember = new_list;
				DataMonitor.updateData( mEditingMember, VNPC.RECORDGROUP_EDITING );
				allMembers.onDataUdt( { } );
			}
		}
		
		/** 结束编辑，提交新增 */
		ns_interactive_slot function addEditedGroup( button:ui_button ):void
		{
			if ( nameText.length == 0 )
			{
				notify( "请至少输入分组名称" );
				return;
			}
			
			var group:StructGroup = new StructGroup( -1 );
			group.name = nameText.text;
			group.copyMembersFrom( mEditingMember );
			
			var result:int;
			if ( mCurIdx < 0 )
			{
				result = DataAccess.instance.queryGroupInterf().insert( group );
				if ( result < 0 ) notify( "和已有记录相同，无法加入" );
			}
			else 
			{
				var groups:Vector.<StructGroup> = DataMonitor.getData( DataAccess.instance.queryGroupInterf().vname );
				var editing:StructGroup = groups[mCurIdx];
				result = DataAccess.instance.queryGroupInterf().update( editing.ID, group );
				if ( result < 0 ) notify( "记录出错，请重新输入" );
			}
			if ( result >= 0 ) ns_interactive_slot::exitEditing( button );
		}
		
		/** 设置tree的tags */
		ns_bindattr_filter function setGroupTag( allGroup:Vector.<StructGroup> ):Array
		{
			var arr:Array = [], len:int = allGroup.length;
			for ( var i:int = 0; i < len; ++i )
				arr.push( allGroup[i].toXML() );
			return arr;
		}
		
		/** 显示列表头 */
		ns_group_handler function showGroupNode( tree:ui_tree_tag, node:uiUnit, tag:String ):void
		{
			var title:UGroupEntry = new UGroupEntry();
			var groups:Vector.<StructGroup> = DataMonitor.getData( DataAccess.instance.queryGroupInterf().vname );
			title.iNameText.component.text = groups[node.key].name;
			title.iEditButton.component.click = "editThisGroup";
			title.iDeleteButton.component.click = "delThisGroup";
			if ( tree.isTagExpand(tag) )
			{
				title.iExpandButton.component.text = "收起";
				title.iExpandButton.component.click = "closeGroupNode";
				title.iEditButton.visible = false;
				title.iDeleteButton.visible = false;
			}
			else
			{
				title.iExpandButton.component.text = groups[node.key].members.length + "人";
				title.iExpandButton.component.click = "expandGroupNode";
			}
			initUIContainer( title ) || node.addChild( title );
		}
		
		/** 展开分组节点后显示的内容 */
		ns_group_handler function showGroupMember( node:uiUnit, tag:String ):DisplayObject
		{
			var groups:Vector.<StructGroup> = DataMonitor.getData( DataAccess.instance.queryGroupInterf().vname );
			var currgrp:StructGroup = groups[ node.key ];
			DataMonitor.updateData( currgrp.members, VNPC.RECORDGROUP_EXPANDED );
			return mExpandedList;
		}
		
		/** 点击按钮，展开按钮所在分组 */
		ns_interactive_slot function expandGroupNode( button:ui_text ):void
		{
			var all_tag_of_list:Array = groupList.tags;
			for each( var tag_of_list:String in all_tag_of_list )
				groupList.closeTag( tag_of_list );
			
			var node:uiUnit = button.parent.parent.parent as uiUnit;
			groupList.expandTag( node.key );
			groupList.doSelect( node.key );
		}
		
		/** 点击按钮，关闭按钮所在分组 */
		ns_interactive_slot function closeGroupNode( button:ui_text ):void
		{
			var node:uiUnit = button.parent.parent.parent as uiUnit;
			groupList.closeTag( node.key );
			groupList.doSelect( -1 );
		}
		
		/** 展开分组人员，显示人员信息 */
		ns_group_handler function listExpanded( entry:uiUnit, staffID:int ):void
		{
			var staff:StructStaff = DataAccess.instance.queryStaffInterf().getByID( staffID ) as StructStaff;
			var content:UGroupExpandMember = new UGroupExpandMember();
			content.iIndexText.component.text = entry.key + 1;
			content.iNameText.component.text = staff.name + "@" + staff.identity;
			content.iDeleteButton.component.click = "delThisMember";
			initUIContainer( content );
			entry.addChild( content );
		}
		
		/** 点击编辑当前分组 */
		ns_interactive_slot function editThisGroup( button:ui_button ):void
		{
			var groups:Vector.<StructGroup> = DataMonitor.getData( DataAccess.instance.queryGroupInterf().vname );
			var entry:uiUnit      = button.parent.parent.parent as uiUnit;
			mCurIdx               = entry.key;
			var group:StructGroup = groups[mCurIdx];
			okButton.text         = "保存编辑";
			finishButton.click    = "exitEditing";
			nameText.text         = group.name;
			setToEditingMembers( group.members );
			DataMonitor.notify( VNPC.RECORDGROUP_EDITING );
			allMembers.onDataUdt( { } );
		}
		
		/** 点击删除当前分组 */
		ns_interactive_slot function delThisGroup( button:ui_button ):void
		{
			var groups:Vector.<StructGroup> = DataMonitor.getData( DataAccess.instance.queryGroupInterf().vname );
			var entry:uiUnit      = button.parent.parent.parent as uiUnit;
			var group:StructGroup = groups[entry.key];
			
			var result:int = DataAccess.instance.queryGroupInterf().remove( group.ID );
			ASSERT( result >= 0 );
		}
		
		/** 点击保存当前编辑分组 */
		ns_interactive_slot function exitEditing( button:ui_button ):void
		{
			nameText.text      = "";
			okButton.text      = "新增班次";
			mCurIdx       	   = -1;
			finishButton.click = "switchChild";
			mEditingMember.length = 0;
			DataMonitor.notify( VNPC.RECORDGROUP_EDITING );
			allMembers.onDataUdt( { } );
		}
		
		/** 点击“删除”把当前成员删掉 */
		ns_interactive_slot function delThisMember( button:ui_button ):void
		{
			var groups:Vector.<StructGroup> = DataMonitor.getData( DataAccess.instance.queryGroupInterf().vname );
			var group:StructGroup = groups[ groupList.currentKey ];
			var entry:uiUnit = button.parent.parent.parent as uiUnit;
			var members:Array = group.members;
			members.splice( entry.key, 1 );
			group.copyMembersFrom( members );
			mExpandedList.hook = "updateGroupList";
			DataMonitor.updateData( group.members, VNPC.RECORDGROUP_EXPANDED );
		}
		
		ns_group_hook function updateGroupList( group:uiGroup ):void
		{
			mExpandedList.hook = null;
			groupList.show_display();
		}
		
		//交互组件定义
		//------------------------------------
		public function get closeButton():ui_icon_button
		{
			return mFrame.iCloseButton.component;
		}
		public function get groupMembers():uiGroup
		{
			return mFrame.iGroupMembers.component;
		}
		public function get groupBar():ui_scroll_bar
		{
			return mFrame.iGroupBar.component;
		}
		public function get allMembers():uiGroup
		{
			return mFrame.iAllMembers.component;
		}
		public function get allBar():ui_scroll_bar
		{
			return mFrame.iAllBar.component;
		}
		public function get addButton():ui_icon_button
		{
			return mFrame.iAddButton.component;
		}
		public function get delButton():ui_icon_button
		{
			return mFrame.iDelButton.component;
		}
		public function get okButton():ui_button
		{
			return mFrame.iOkButton.component;
		}
		public function get finishButton():ui_button
		{
			return mFrame.iFinishButton.component;
		}
		public function get nameText():UserInput
		{
			return mFrame.iNameText.component;
		}
		public function get groupList():ui_tree_tag
		{
			return mFrame.iGroupList.component;
		}
		public function get scrollBar():ui_scroll_bar
		{
			return mFrame.iScrollBar.component;
		}
	}

}